// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#pragma once

#include <melon/string.h>
#include <melon/init/init.h>
#include <gtest/gtest.h>
#include <string>
#include <unordered_set>
#include <vector>

#include <pollux/common/file/file_systems.h>
#include <pollux/exec/aggregate.h>
#include <pollux/testing/exec/aggregation_fuzzer_options.h>
#include <pollux/testing/exec/window_fuzzer.h>
#include <pollux/testing/expression/fuzzer_toolkit.h>
#include <pollux/parse/type_resolver.h>
#include <pollux/serializers/presto_serializer.h>
#include <pollux/vector/vector_fuzzer.h>

namespace kumo::pollux::exec::test {

class WindowFuzzerRunner {
 public:
  static int run(
      size_t seed,
      std::unique_ptr<ReferenceQueryRunner> referenceQueryRunner,
      const AggregationFuzzerOptions& options) {
    return runFuzzer(
        seed, std::nullopt, std::move(referenceQueryRunner), options);
  }

  static int runRepro(
      const std::optional<std::string>& planPath,
      std::unique_ptr<ReferenceQueryRunner> referenceQueryRunner) {
    return runFuzzer(0, planPath, std::move(referenceQueryRunner), {});
  }

 protected:
  static int runFuzzer(
      size_t seed,
      const std::optional<std::string>& planPath,
      std::unique_ptr<ReferenceQueryRunner> referenceQueryRunner,
      const AggregationFuzzerOptions& options) {
    auto aggregationSignatures =
        kumo::pollux::exec::getAggregateFunctionSignatures();
    auto windowSignatures = kumo::pollux::exec::windowFunctions();
    if (aggregationSignatures.empty() && windowSignatures.empty()) {
      KLOG(ERROR) << "No function registered.";
      exit(1);
    }

    auto filteredAggregationSignatures = pollux::fuzzer::filterSignatures(
        aggregationSignatures, options.onlyFunctions, options.skipFunctions);
    auto filteredWindowSignatures = pollux::fuzzer::filterSignatures(
        windowSignatures, options.onlyFunctions, options.skipFunctions);
    if (filteredAggregationSignatures.empty() &&
        filteredWindowSignatures.empty()) {
      KLOG(ERROR)
          << "No function left after filtering using 'only' and 'skip' lists.";
      exit(1);
    }

    kumo::pollux::parse::registerTypeResolver();
    kumo::pollux::serializer::presto::PrestoVectorSerde::
        registerVectorSerde();
    kumo::pollux::filesystems::registerLocalFileSystem();

    auto& aggregationFunctionDataSpecs =
        referenceQueryRunner->aggregationFunctionDataSpecs();

    kumo::pollux::exec::test::windowFuzzer(
        filteredAggregationSignatures,
        filteredWindowSignatures,
        seed,
        options.customVerificationFunctions,
        options.customInputGenerators,
        options.orderDependentFunctions,
        aggregationFunctionDataSpecs,
        options.timestampPrecision,
        options.queryConfigs,
        options.hiveConfigs,
        options.orderableGroupKeys,
        planPath,
        std::move(referenceQueryRunner));
    // Calling gtest here so that it can be recognized as tests in CI systems.
    return RUN_ALL_TESTS();
  }
};

} // namespace kumo::pollux::exec::test
